/**
 * Created by jun on 15/8/14.
 */
var AV = require('leanengine');
var async = require('async');
var UserM=require('../models/modUser.js');

var config = require('../config.js');
//商品有关的模型
var UserM=require('../models/modUser.js');
module.exports = {
    /*
    * 根据用户openid获取积分，余额，头像
    * */
    getPointsAmountByOpenID:function(req,res,next){
        console.log('123');
        var openid = req.AV.user.get('openid');
        console.log(openid);
       // console.log('123456789000'+openid);
        //console.log(openid);
        var user = new Object();

        try{
            async.parallel([
                function(callback){
                    //console.log('1');
                    UserM.getPointsByOpenID(openid).then(function(point){
                        user.points=point;
                        callback(null,point);
                    })
                },
                function(callback){
                    //console.log('2');
                    UserM.getAmountByOpenID(openid).then(function(amount){
                        user.amount=amount;
                        callback(null,amount);
                    })
                },
                function(callback){
                    //console.log('3');
                    UserM.getUserHeadIMG(openid).then(function(headimg){
                        user.headimg=headimg;
                        callback(null,headimg);
                    })
                },
                function(callback){
                    //console.log('4');
                    UserM.getNicknameByOpenid(openid).then(function(nickname){
                        user.nickname=nickname;
                        callback(null,nickname);
                    })
                }
            ],function(err,result){
                if(err){
                    //TODO:
                    return;
                }
                //console.log('5');
                res.render('user/center',{user:user,title:'用户中心'});
            });

            //user.points=UserM.getPointsByOpenID(openid);
            //
            //console.log('123456789000'+UserM.getPointsByOpenID(openid));
            //
            //user.amount=UserM.getAmountByOpenID(openid);
            //console.log('123456789111'+UserM.getAmountByOpenID(openid));
            //
            //user.headimg=UserM.getUserHeadIMG(openid);
            //res.render('user/center',{user:user,title:'用户中心'});
        }catch(e){
            console.log(e);
        }
    },
    center:function(req, res, next){
        res.render("user/center",{title:'个人中心'});
    },
    member:function(req, res, next){
        res.render("user/member",{title:'我的会员'});
    },
    history:function(req, res, next){
        res.render("user/history",{title:'个人订单'});
    },
    coupon:function(req, res, next){
        res.render("user/coupon",{title:'我的优惠券'});
    },
    groupHistory:function(req, res, next){
        res.render("user/groupHistory",{title:'团购订单'});
    },
    collect:function(req, res, next){
        res.render("user/collect",{title:'我的收藏'});
    }


};
